import * as React from 'react';

import {IView} from '../types/IView';
import {IAttr} from '../types/IAttr';

import {IViewObject} from './IViewObject';
import AbsParentViewObject from './AbsParentViewObject';
import isParentView from '../types/IParentView';

export class CustomViewObject extends AbsParentViewObject<IView> implements IViewObject<IView> {

  viewType = 'custom';

  attrs: IAttr[] = [];

  create(designId: string, templateView: IView): IView {
    const view = JSON.parse(JSON.stringify(templateView));
    this.updateView(view, designId);

    return view;
  }

  render(view: IView, refUpdater: (view: IView, ref: Element) => void): JSX.Element | null {
    return;
  }

  private updateView(view: IView, designId: string) {
    view.id = this.generateId();
    view.designId = designId;

    if (isParentView(view)) {
      (view.children || []).forEach(childView => this.updateView(childView, designId));
    }
  }
}

const customViewObject = new CustomViewObject();
export default customViewObject;
